package com.itheima.test;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        lo:while(true){
            System.out.println("------欢迎来到学生管理系统------");
            System.out.println("1 增加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");

            String choice = sc.next();

            switch (choice){
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    removeStudent(list);
                    break;
                case "3":
                    setStudent(list);
                    break;
                case "4":
                    queryStudent(list);
                    break;
                case "5":
                    System.out.println("退出");
                    break lo;
            }
        }
    }
    //查看学生的方法
    private static void queryStudent(ArrayList<Student> list) {
        if(list.size() == 0){
            System.out.println("没有任何数据,添加后查询");
            return;
        }
        System.out.println("学号\t\t姓名\t\t年龄\t\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid()+"\t\t"+stu.getName()+"\t\t"+stu.getAge()+"\t\t"+stu.getBirthday());
        }
    }

    //添加学生的方法
    private static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);

        String sid;
        while (true){
            System.out.println("请输入学生学号:");
            sid = sc.next();
            int index = getIndex(list,sid);
            if(index == -1){
                break;
            }
        }
        System.out.println("请输入学生姓名:");
        String username = sc.next();
        System.out.println("请输入学生的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生的生日");
        String birthday = sc.next();

        Student stu = new Student(sid,username,age,birthday);
        list.add(stu);
        System.out.println("添加成功");
    }

    //获取索引用于删除或修改
    public static int getIndex(ArrayList<Student> list,String sid){
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if(stu.getSid().equals(sid)){
                index = i;
                return index;
            }
        }
        return index;
    }

    //删除学生的方法
    public static void removeStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入要删除学生的学号:");
        String sid = sc.next();

        int index = getIndex(list,sid);

        if(index == -1){
            System.out.println("对不起,您输入的学号不存在");
            return;
        }else{
            list.remove(index);
            System.out.println("删除成功");
        }
    }

    //修改学生的方法
    public static void setStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入要修改学生信息的学号:");
        String sid = sc.next();

        int index = getIndex(list, sid);

        if(index == -1){
            System.out.println("对不起,您要修改的学生信息不存在");
            return;
        } else {
            System.out.println("请输入学生姓名:");
            String username = sc.next();
            System.out.println("请输入学生年龄");
            int age = sc.nextInt();
            System.out.println("请输入学生的生日");
            String birthday = sc.next();
            Student stu = new Student(sid,username,age,birthday);
            list.set(index,stu);
            System.out.println("修改成功");
        }
    }
}
